<template>
  <div class="register">
    <div class="card">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="85px"
        class="demo-ruleForm myfrom"
        run
      >
        <h1>欢迎加入智能家政服务平台</h1>
        <h5>用户注册</h5>

        <el-form-item label="用户名：" prop="username">
          <el-input type="text" clearable v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <label slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
          <el-input type="password" show-password v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码：" prop="checkPass">
          <el-input type="password" show-password v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="sexSelect">
            <label class="sex_style">性&nbsp;&nbsp; 别:</label>
            <div class="sex_radio radio__label">
              <el-radio v-model="ruleForm.sex" label="男">男</el-radio>
              <el-radio v-model="ruleForm.sex" label="女">女</el-radio>
            </div>
          </div>
        </el-form-item>
        <el-form-item label=" 手机号码：">
          <el-input type="tel" v-model="ruleForm.tel"></el-input>
        </el-form-item>
        <el-form-item label=" 验证码:" prop="code">
          <label slot="label">&nbsp;&nbsp;验证码:</label>
          <el-input clearable v-model="ruleForm.code" maxlength="5">
            <template #append>
              <el-button @click="getCaptcha">刷新验证码</el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="code_img">
          <el-image
            style=" width: 158px;height: 42px ;margin-left:50px"
            :src="captchaImg"
            class="captchaImg"
            @click="getCaptcha"
          ></el-image>
        </el-form-item>
        <!-- <el-form-item label="验证码:" prop="code">
       <el-input class="code_input" v-model="ruleForm.code "></el-input>
       <el-input class="code"  >12345</el-input>
        </el-form-item>-->
        <el-form-item label="默认地址：">
          <el-input type="text" v-model="ruleForm.address"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="mar_del" type="primary" @click="submitForm('ruleForm')">用户注册</el-button>
          <el-button class="mar_del" type="primary" @click="goHomeRegister">家政公司注册</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          <!-- <el-button class="mar_del_back" @click="resetForm('ruleForm')">返回</el-button>  -->
          <el-button class="mar_del_back" @click="backForm()">返回</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <router-link
            to="/home"
            class="btn btn-outline-primary"
            style="margin-right:5px;"
            tag="button"
          >回到首页</router-link>
          <router-link to="/login" class="btn btn-outline-primary" tag="button">已有账号</router-link>
        </el-form-item>-->
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from 'qs'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      captchaImg: '',
      token: '',
      ruleForm: {
        username: '',
        password: '',
        checkPass: '',
        sex: '',
        tel: '',
        code: '',
        address: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data } = await this.$http.post(
            '/sys/user/register?token=' +
              this.token +
              '&code=' +
              this.ruleForm.code,
            {
              username: this.ruleForm.username,
              password: this.ruleForm.password,
              sex: this.ruleForm.sex,
              phone: this.ruleForm.phone,
              address: this.ruleForm.address,
            }
          )

          if (data.code != 200) {
            this.$refs[formName].resetFields()
            this.$message.error('注册失败！')
          } else {
            this.$refs[formName].resetFields()
            this.$message.success('注册成功！')

            this.$router.push('/login')
          }
        }
      })
    },
    goHomeRegister() {
      this.$router.push('/homeRegister')
    },
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },
    backForm() {
      this.$router.push('/login')
    },

    suer() {
      if (this.ruleForm.checkPass != this.ruleForm.password) {
        this.$message.error('两次密码不同！！')
      }
    },
    getCaptcha() {
      this.$http.get('/captcha').then((res) => {
        this.token = res.data.data.token
        this.captchaImg = res.data.data.captchaImg
        this.ruleForm.code = ''
      })
    },
  },
  created() {
    this.getCaptcha()
  },
}
</script>

<style lang="less" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 120vh;

  //  background: url("~@/assets/imags/login/login.jpg") center center no-repeat;
  background: url('~@/assets/imags/login/login.jpeg') no-repeat;
  background-size: cover;
}
.card {
  width: 28rem;
  padding: 20px;
  background: rgba(255, 255, 255, 0.35);
  border: 0.5px solid rgba(127, 255, 212, 0.3);
}
.el-form {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
.el-message {
  top: 200px !important;
}
h1 {
  // letter-spacing: 6px;
  font-size: 18px;
  margin-top: 8px;
  color: #888888;
}
h2 {
  margin-top: 5px;
}
.btn-outline-primary {
  color: #fff !important;
}
.el-input--suffix {
  width: 220px !important;
}
.el-form-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-bottom: 18px;
}
.sexSelect {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  margin-left: 20px;
}
.el-form-item__content {
  display: flex !important;
}
.sex_style {
  margin-right: 130px !important;
  // margin-bottom: 22px;
}
.sex_radio {
  position: relative;
  right: 106px;
}
.radio__label {
  font-size: 17px;
  padding-left: 10px;
}
</style>
